package com.anda.driver.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URLDecoder;

public class SerializeTool {
	public static String object2String(Object obj)  {  
		String objBody = null;  
		ByteArrayOutputStream baops = null;  
		ObjectOutputStream oos = null;  
 
		try  
        {  
			baops = new ByteArrayOutputStream();  
            oos = new ObjectOutputStream(baops);  
            oos.writeObject(obj);  
            byte[] bytes = baops.toByteArray();  
            objBody = new String(bytes);  
        } catch (IOException e)  
        {  
            Constant.debug(e.toString());
        } finally  
        {  
            try  
            {  
                if (oos != null)  
                    oos.close();  
                if (baops != null)  
                    baops.close();  
            } catch (IOException e)  
            {  
            	Constant.debug(e.toString());
            }  
        }  
        return objBody;  
    }
	  
	    @SuppressWarnings("unchecked")  
	    public static <T extends Serializable> T getObjectFromString   
	                    (String objBody, Class<T> clazz)  
	  
	    {  
	        byte[] bytes = objBody.getBytes();  
	        ObjectInputStream ois = null;  
	        T obj = null;  
	        try  
	        {  
	            ois = new ObjectInputStream(new ByteArrayInputStream(bytes));  
	            obj = (T) ois.readObject();  
	        } catch (IOException e)  
	        {  
	        	Constant.debug(e.toString());
	        } catch (ClassNotFoundException e)  
	        {  
	        	Constant.debug(e.toString());
	        } finally  
	        {  
	  
	            try  
	            {  
	                if (ois != null)  
	                    ois.close();  
	            } catch (IOException e)  
	            {  
	            	Constant.debug(e.toString());
	            }  
	        }  
	  
	        return obj;  
	    }  
	    
	    private static String decodeUrlStr(String s, boolean decode){
			if (s.equals("null")) return null;
			try {
				if (decode) return URLDecoder.decode(s, "utf-8");
				return s;
			} catch (Exception e){
				return null;
			}
		}
	    
	public static void main(String [] args){
		String s = "%25EF%25BF%25BD%25EF%25BF%25BD%2500%2505sr%2500%2524com.anda.driver.locator.LocationInfo%252C%2500%2503i%25EF%25BF%25BDB%25EF%25BF%25BD-%2502%2500%2515F%2500%2508accuracyJ%2500%2507getTimeI%2500%2502idI%2500%2506isSentD%2500%2503latD%2500%2503lngL%2500%250BaccessTokent%2500%2512Ljava%252Flang%252FString%253BL%2500%250DaddressDetailq%2500%257E%2500%2501L%2500%2504cityq%2500%257E%2500%2501L%2500%2508cityCodeq%2500%257E%2500%2501L%2500%2507countryq%2500%257E%2500%2501L%2500%250BcountryCodeq%2500%257E%2500%2501L%2500%250BfeatureNameq%2500%257E%2500%2501L%2500%250ClocationDescq%2500%257E%2500%2501L%2500%2506regionq%2500%257E%2500%2501L%2500%2506streetq%2500%257E%2500%2501L%2500%250CstreetNumberq%2500%257E%2500%2501L%2500%250CsubAdminAreaq%2500%257E%2500%2501L%2500%250BsubLocalityq%2500%257E%2500%2501L%2500%250Cthoroughfareq%2500%257E%2500%2501L%2500%2504typeq%2500%257E%2500%2501xp%2500%2500%2500%2500%2500%2500%2501%253B2%255C%25EF%25BF%25BD%25EF%25BF%25BD%2500%2500%2500%2500%2500%2500%2500%2500%25408%25EF%25BF%25BDu%25EF%25BF%25BD%2521%25EF%25BF%25BDS%2540Y%25EF%25BF%25BD%25EF%25BF%25BDU%255CR%25EF%25BF%25BDpt%2500%252C%25E4%25B8%25AD%25E5%259B%25BD%252C%25E4%25BA%2591%25E5%258D%2597%25E7%259C%2581%25E6%2598%2586%25E6%2598%258E%25E5%25B8%2582%25E5%25AE%2598%25E6%25B8%25A1%25E5%258C%25BA%252C%25E8%25BF%258E%25E5%25AE%25BE%25E8%25B7%25AFt%2500%2509%25E6%2598%2586%25E6%2598%258E%25E5%25B8%2582pt%2500%2506%25E4%25B8%25AD%25E5%259B%25BDt%2500%2502CNt%2500%2509%25E8%25BF%258E%25E5%25AE%25BE%25E8%25B7%25AFpt%2500%2509%25E4%25BA%2591%25E5%258D%2597%25E7%259C%2581pppt%2500%2509%25E5%25AE%2598%25E6%25B8%25A1%25E5%258C%25BAq%2500%257E%2500%2507t%2500%2504test";
		String s1 = decodeUrlStr(s, true);
		System.out.println(s1);
	}
}
